package org.link.ui;

import java.awt.Insets;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.link.dao.MysqlDao;
import org.link.util.Util;
import javax.swing.JButton;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.SwingConstants;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 
 * project_name:jksystem
 * package_name:org.link.ui
 * file_name:LoginFrame.java
 * @author Link
 * create_date:2015年9月19日下午12:46:26
 * modified_date:
 * description: 登录窗口
 *
 */
public class LoginFrame extends JFrame {

	private JPanel mainPane;				//主面板
	private JButton registButton;			//注册按钮
	private JButton userLoginButton;		//用户登录按钮
	private JButton managerLoginButton;		//用户管理员登录按钮
	private JTextField nameField;			//用户名文本框
	private JPasswordField passwordField;	//密码框
	private JLabel passwordLabel;			//密码标签
	private JButton cancelButton;			//撤销按钮
	private JButton loginsureButton;		//用户登录确定按钮
	private JButton managersureButton;		//管理员确定按钮
	private JButton registsureButton;		//注册确定按钮
	private JLabel promptLabel;				//提示标签
	private JButton checkButton;			//用户名检测按钮
	private JButton returnButton;			//返回上层按钮
	private JLabel nameLabel;				//用户名标签
	private MysqlDao mysqlDao = new MysqlDao();

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 334, 310);
		setResizable(false);
		setTitle("驾考系统");
		Util.screen(this);
		mainPane = new JPanel();
		mainPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(mainPane);
		mainPane.setLayout(null);
		
		nameLabel = new JLabel("用户名: ");
		nameLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		nameLabel.setBounds(22, 39, 54, 31);
		
		
		nameField = new JTextField();
		nameField.setBounds(86, 41, 126, 30);
		
		/**
		 * 检测账号是否可用按钮
		 */
		checkButton = new JButton("是否可用");
		checkButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		checkButton.setBounds(222, 41, 93, 31);
		checkButton.setMargin(new Insets(0, 0, 0, 0));
		checkButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(mysqlDao.queryUserExists(nameField.getText())){
					LoginFrame.this.promptLabel.setText("该用户名已注册,请重新输入!");
				}else{
					LoginFrame.this.promptLabel.setText("该用户名可用!");
				}
			}
		});
		/**
		 * 返回上次按钮
		 */
		returnButton = new JButton("返回上层");
		returnButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		returnButton.setMargin(new Insets(0, 0, 0, 0));
		returnButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();
				mainPane.repaint();
				mainPane.add(userLoginButton);
				mainPane.add(registButton);
				mainPane.add(managerLoginButton);
				LoginFrame.this.nameField.setText("");
				LoginFrame.this.passwordField.setText("");
				LoginFrame.this.promptLabel.setText("");
				LoginFrame.this.setTitle("驾考系统");
				
			}
		});
		
		passwordLabel = new JLabel("密  码: ");
		passwordLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		passwordLabel.setBounds(22, 98, 54, 31);
		
		
		passwordField = new JPasswordField();
		passwordField.setBounds(86, 98, 126, 31);
		
		
		promptLabel = new JLabel();
		promptLabel.setForeground(Color.RED);
		promptLabel.setHorizontalAlignment(SwingConstants.CENTER);
		promptLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		promptLabel.setBounds(15, 143, 300, 31);
		
		/**
		 * 撤销按钮
		 */
		cancelButton = new JButton("撤  销");
		cancelButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		cancelButton.setBounds(36, 192, 86, 47);
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				nameField.setText("");
				passwordField.setText("");
			}
		});
		
		/**
		 * 注册用户确定按钮
		 */
		registsureButton = new JButton("确  定");
		registsureButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		registsureButton.setBounds(179, 192, 86, 47);
		registsureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameField.getText();
				String password = new String(passwordField.getPassword());
				if(mysqlDao.queryUserExists(name)){
					LoginFrame.this.promptLabel.setText("该用户名已存在,请重新输入!");
				}else{
					if(mysqlDao.addUser(name, password) == 1){
						LoginFrame.this.promptLabel.setText("注册成功,请牢记密码: "+password);
					}else{
						LoginFrame.this.promptLabel.setText("注册失败!");
					}
				}
				
			}
		});
		
		/**
		 * 学员登录确定按钮
		 */
		loginsureButton = new JButton("确  定");
		loginsureButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		loginsureButton.setBounds(179, 192, 86, 47);
		loginsureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameField.getText();
				String password = new String(passwordField.getPassword());
				if(mysqlDao.queryUserExists(name, password)){//账号密码正确进行界面跳转
					LoginFrame.this.dispose();//关闭当前窗口
					MainFrame mainFrame = new MainFrame(name);
					mainFrame.setTitle("驾考系统_学员");
					mainFrame.setVisible(true);//跳转到主面板窗口
				}else{
					LoginFrame.this.promptLabel.setText("账号或密码错误,请重新输入!");
				}
			}
		});
		
		/**
		 * 管理员登录确定按钮
		 */
		managersureButton = new JButton("确  定");
		managersureButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		managersureButton.setBounds(179, 192, 86, 47);
		managersureButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String name = nameField.getText();
				String password = new String(passwordField.getPassword());
				if(mysqlDao.queryManagerExists(name, password)){//账号密码正确进行界面跳转
					LoginFrame.this.dispose();//关闭当前窗口
					MainFrame mainFrame = new MainFrame(name);
					mainFrame.setTitle("驾考系统_管理员");
					mainFrame.setVisible(true);//跳转到主面板窗口
				}else{
					LoginFrame.this.promptLabel.setText("账号或密码错误,请重新输入!");
				}
			}
		});
		
		/**
		 * 注册用户
		 */
		registButton = new JButton("注册学员");
		registButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		registButton.setBounds(105, 177, 119, 36);
		mainPane.add(registButton);
		registButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				mainPane.removeAll();//移除面板
				mainPane.repaint();//重置面板
				mainPane.add(registsureButton);
				mainPane.add(cancelButton);
				mainPane.add(promptLabel);
				mainPane.add(passwordField);
				mainPane.add(passwordLabel);
				mainPane.add(checkButton);
				mainPane.add(nameField);
				mainPane.add(nameLabel);
				LoginFrame.this.returnButton.setBounds(222, 98, 93, 31);
				mainPane.add(returnButton);
				LoginFrame.this.setTitle("注册学员窗口");
			}
		});
		
		/**
		 * 登录用户按钮
		 */
		userLoginButton = new JButton("登录学员");
		userLoginButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		userLoginButton.setBounds(105, 44, 119, 36);
		mainPane.add(userLoginButton);
		userLoginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();//移除面板
				mainPane.repaint();//重置面板
				mainPane.add(loginsureButton);
				mainPane.add(cancelButton);
				mainPane.add(promptLabel);
				mainPane.add(passwordField);
				mainPane.add(passwordLabel);
				mainPane.add(nameField);
				mainPane.add(nameLabel);
				LoginFrame.this.returnButton.setBounds(222, 41, 93, 31);
				mainPane.add(returnButton);
				LoginFrame.this.setTitle("登录学员窗口");
			}
		});
		
		/**
		 * 管理员登录
		 */
		managerLoginButton = new JButton("登录管理员");
		managerLoginButton.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		managerLoginButton.setBounds(105, 108, 119, 36);
		mainPane.add(managerLoginButton);
		managerLoginButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				mainPane.removeAll();//移除面板
				mainPane.repaint();//重置面板
				mainPane.add(managersureButton);
				mainPane.add(cancelButton);
				mainPane.add(promptLabel);
				mainPane.add(passwordField);
				mainPane.add(passwordLabel);
				mainPane.add(nameField);
				mainPane.add(nameLabel);
				LoginFrame.this.returnButton.setBounds(222, 41, 93, 31);
				mainPane.add(returnButton);
				LoginFrame.this.setTitle("登录管理员窗口");
			}
		});
	}
}








